package com.chenfan.common.vo;

import com.chenfan.common.config.Constant;
import com.chenfan.common.exception.ResultState;
import com.chenfan.common.exception.SystemState;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.List;

/**
 * 统一返回对象
 *
 * @author lizhejin
 */
@ApiModel
public class Response<T> {

    /**
     * 返回码
     */
    @ApiModelProperty(position = 1, value = "返回码")
    private int code;
    /**
     * 返回文字内容
     */
    @ApiModelProperty(position = 2, value = "返回信息")
    private String message;
    /**
     * 返回实体对象
     */
    @ApiModelProperty(position = 3, value = "返回体")
    private T obj;

    public Response(int code, String message, T obj) {
        this.code = code;
        this.message = message;
        this.obj = obj;
    }

    public Response() {
    }

    public Response(Exception ex) {
        this.code = Constant.EXCEPTION;
        this.message = ex.getMessage();
    }

    public Response(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public Response(ResponseCode responseCode) {
        this.code = responseCode.getCode();
        this.message = responseCode.getMsg();
    }

    public Response(ResponseCode responseCode, T obj) {
        this.code = responseCode.getCode();
        this.message = responseCode.getMsg();
        this.obj = obj;
    }

    public static <T> Response<T> error(ResultState state) {
        return new Response(state, null);
    }

    public static <T> Response<T> error(int code, String msg) {
        Response response = new Response();
        response.setCode(code);
        response.setMessage(msg);
        return response;
    }

    public Response(ResultState state, final T obj) {
        this.code = state.code();
        this.message = state.message();
        this.obj = obj;
    }

    /**
     * pagerHelp 分页
     */
    public static final <T> Response successWithPage(List<T> pagerList) {
        PageInfo<?> pageInfo = new PageInfo<T>(pagerList);
        Response response = new Response(SystemState.SUCCESS, pageInfo);
        return response;
    }

    /**
     * success
     */
    public static final <T> Response success(T obj) {
        Response response = new Response(SystemState.SUCCESS, obj);
        return response;
    }

    /**
     * error
     */
    public static final <T> Response error(T obj) {
        Response response = new Response(SystemState.FAILURE, obj);
        return response;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getObj() {
        return obj;
    }

    public void setObj(T obj) {
        this.obj = obj;
    }

    public boolean isSuccess() {
        return SystemState.SUCCESS.code() == this.getCode() || 0 == this.getCode();
    }
}
